package com.lbs.jiepiaisi.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.bean.EquipBean;
import com.lbs.jiepiaisi.utils.ToastUtil;

import java.util.List;

/**
 * Created by yu on 2019-12-24 14:07.
 * Description:
 */
public class EquipListAdapter extends BaseQuickAdapter<EquipBean.Result.Items, BaseViewHolder> {

    private List<EquipBean.Result.Items> equipsList;

    public EquipListAdapter(@LayoutRes int layoutResId, @Nullable List<EquipBean.Result.Items> data) {
        super(layoutResId, data);
        this.equipsList = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, EquipBean.Result.Items item) {
        helper.setText(R.id.item_name, "姓名："+item.getOwnerName() == null ? "" : item.getOwnerName())
                .setText(R.id.item_num, "设备号："+item.getEqNo() == null ? "" : item.getEqNo())
                .setText(R.id.item_date, item.getCnStatus().substring(0,2))
                .addOnClickListener(R.id.liner_history) // 添加点击事件
                .addOnClickListener(R.id.liner_navi)
                .addOnClickListener(R.id.liner_track)
                .addOnClickListener(R.id.liner_notice)
                .addOnClickListener(R.id.liner_user_content);

        TextView item_date = helper.getView(R.id.item_date);

        if ("2".equals(item.getStatus())) {
            if (!item.getCnStatus().contains("静止")){
                item_date.setTextColor(mContext.getResources().getColor(R.color.red));
            } else {
                item_date.setTextColor(mContext.getResources().getColor(R.color.deep_gray));
            }
        } else if ("3".equals(item.getStatus())){
            item_date.setText("未启用");
            item_date.setTextColor(mContext.getResources().getColor(R.color.deep_gray));
        } else {
            item_date.setTextColor(mContext.getResources().getColor(R.color.deep_gray));
        }

        ImageView item_image = helper.getView(R.id.item_image);
        // 设置图标
        if ("3".equals(item.getStatus()) ) {
            item_image.setImageResource(R.mipmap.inactive_device);
        } else if ("2".equals(item.getStatus())){
            item_image.setImageResource(R.mipmap.offline_device);
        } else {
            item_image.setImageResource(R.mipmap.online_device);
        }

        helper.getView(R.id.lin_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!"3".equals(item.getStatus())) {
                    if(item.getMark().equals("D")){
                        ToastUtil.show("该设备已经被禁用");
                        return;
                    }
                    boolean isMore = item.isMore();
                    for (EquipBean.Result.Items item : equipsList) {
                        item.setMore(false);
                    }
                    item.setMore(!isMore);
                    notifyDataSetChanged();
                }else {
                    ToastUtil.show("该设备未启用");
                }
            }
        });

        if(item.isMore()){
            //((ItemViewHolder) holder).lin_bottom.startAnimation(AnimationUtils.loadAnimation(context,android.R.anim.fade_in));
            helper.getView(R.id.lin_bottom).setVisibility(View.VISIBLE);
        }else{
            //((ItemViewHolder) holder).lin_bottom.startAnimation(AnimationUtils.loadAnimation(context,android.R.anim.fade_out));
            helper.getView(R.id.lin_bottom).setVisibility(View.GONE);
        }
    }
}
